<template>
	<div class="order-detail" v-if="orderCom.orderItems">
		<div class="order-status">等待支付</div>
		<navigator  v-if="addressInfo" class="address-block" open-type="navigate" :url="`../../address/index?type=select&id=${from}`">
			<div class="send-name">姓名:{{addressInfo.name}} 电话号码: {{addressInfo.phoneNumber}}</div>
			<div class="address">收货地址: {{addressInfo.province}}-{{addressInfo.city}}-{{addressInfo.region}}{{addressInfo.detailAddress}}</div>
		</navigator>
		<div class="order-card">
			<div class="order-title">
				<text>订单编号: {{orderCom.orderBase.orderSn}}</text>
				<text class="status">等待支付</text>
			</div>
			<div class="sku-block" v-for="item in orderCom.orderItems" :key="item.id">
				<image class="img" :src="item.productPic" mode=""></image>
				<div class="info">
					<div class="p1">
						<text class="title">{{item.productName}}</text>
						<text class="price">¥{{item.productPrice | money}}</text>
					</div>
					<div class="p2">
						<text>编号：{{item.productSn}}</text>
						<text>x {{item.productQuantity}}</text>
					</div>
					<div class="p3">
						<text>规格：</text> 
						<text style="margin-right: 4rpx;" v-for="(ele,idx) in item.productAttr" :key="idx">{{ ele.key }} : {{ ele.value }} </text>
					</div>
				</div>
			</div>
			

			<div class="total-price">
				<text>合计: {{orderCom.orderBase.totalAmount | money}}</text>
			</div>
			<!-- buttons -->
			<div class="btns">
				<div class="btn" @click="doPay">确认付款</div>
			</div>
		</div>
	</div>
</template>

<script>
	import orderApi from '@/api/order/index'
	import NP from 'number-precision'
	import mix from "@/mixins/index.js"
	const LEJU_ORDER_KEY = 'leju_order';
	export default {
		name: 'OrderDetail',
		mixins:[mix],
		data() {
			return {
				form:'', // 是否来源于购物车
				order: {},
				address: {},
				addressList:[],
				orderCom:[],
				memberId:""
			}
		},
		onShow(){
			if(this.isLogin){
				this.initSelAddress();
				this.getOrderInfo()
			}
		},
		onLoad(e) {
			this.from = e.id;
			this.memberId = e.memberId;
			
		},
		computed: {
			// orderCom() {
			// 	return this.$store.state.order.tempOrder
			// },
			addressInfo(){
				if(this.memberId){
					return this.addressList.find(ele =>ele.id == this.memberId);
				}
				return this.addressList.find(ele =>ele.defaultStatus == 1)
			}
		},
		methods: {
			getOrderInfo(){
				orderApi.getPreOrderById(this.from)
				.then(res =>{
					res.data.orderItems.forEach(ele =>{
						ele.productAttr = JSON.parse(ele.productAttr)
					})
					this.orderCom = res.data;
				})
			},
			// 初始化预选地址
			initSelAddress(){
				orderApi.findAllAddress()
				.then(res=>{
					this.addressList = res.data.items;
				})
				// var addressObj = uni.getStorageSync("selectAddress");
				// // 做非空验证
				// if(addressObj){
				// 	this.address = {
				// 		id: addressObj.id,
				// 		name: addressObj.name,
				// 		tel: addressObj.tel,
				// 		addressText: addressObj.address.region.label + addressObj.address.region.detailed
				// 	}
				// }
			},
			doPay() {
				
				var obj = {
					  "addressId": this.addressInfo.id,
					  "orderId": this.from,
					  "orderItemList": this.orderCom.orderItems,
					  "payType": 0,
					  "sourceType": 1
				}
				orderApi.addConfirmOrder(obj)
				.then(res =>{
					if(res.success){
						uni.navigateTo({
							url: `../../../pay/payment/payment?orderId=${res.data.orderId}&price=${res.data.totalAmount}&orderSn=${res.data.orderSn}` 
						})
					}
				})
				// uni.showLoading({
				// 	title: '正在提交订单...'
				// })
				// var skuList = {};
				// for(var i = 0 ; i < this.orderCom.orderDetailList.length; i ++){
				// 	var sku = this.orderCom.orderDetailList[i];
				// 		skuList['orderDetail['+i+'].skuId'] = sku.skuId;
				// 		skuList['orderDetail['+i+'].price'] = sku.price;
				// 		skuList['orderDetail['+i+'].count'] = sku.count;
				// }
				// var postData = {
				// 	userId: this.orderCom.userId,
				// 	addressId: this.address.id,
				// 	count: this.orderCom.count,
				// 	price: this.orderCom.price,
				// 	...skuList
				// }
				// orderApi.addOrderBatch(postData)
				// 	.then(res=>{
				// 		if(res.code == 'S'){
				// 			uni.hideLoading();
				// 			// 获取返回的订单id
				// 			var orderId = res.orderId;
				// 			// 提交成功之后, 才执行 根据 from 判断是否来自购物车 如果是 删除购物车内容
				// 			// todo
				// 			uni.navigateTo({
				// 				url: '../../../pay/payment/payment?price=' + this.orderCom.price+'&orderId='+orderId
				// 			})
				// 		}else{
				// 			uni.hideLoading();
				// 			uni.showToast({
				// 				title:'提交失败!'
				// 			})
				// 		}
				// 	})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.order-detail {
		.order-status {
			font-size: 36rpx;
			color: #494949;
			height: 88rpx;
			border-bottom: 1px solid #E5E5E5;
			line-height: 88rpx;
			margin: 0 60rpx;
			box-sizing: border-box;
		}

		.address-block {
			font-size: 28rpx;
			color: #494949;
			border-bottom: 1px solid #E5E5E5;
			margin: 0 60rpx;
			box-sizing: border-box;
			padding: 30rpx 0;
		}

		.order-card {
			width: 672rpx;
			box-sizing: border-box;
			padding: 36rpx;
			border-radius: 24rpx;
			background-color: #fff;
			margin: 30rpx auto;
			font-size: 24rpx;
			color: #3E3E3E;

			.order-title {
				line-height: 33rpx;
				display: flex;
				justify-content: space-between;

				.status {
					color: #034C46;
				}

				border-bottom: 1px solid #F1ECE7;
				padding-bottom: 30rpx;
			}

			.sku-block {
				display: flex;
				justify-content: flex-start;
				border-bottom: 1px solid #F1ECE7;
				padding: 30rpx 0;

				.img {
					width: 162rpx;
					height: 162rpx;
					flex-shrink: 0;
					background-color: #8F8F94;
				}

				.info {
					width: 470rpx;
					margin-left: 28rpx;

					.p1,
					.p2 {
						display: flex;
						justify-content: space-between;
						align-items: center;
					}

					.p1 {
						font-size: 26rpx;
						line-height: 37px;

						.title {
							color: #3E3E3E;
						}

						.price {
							colro: #8D8D8D;
						}
					}

					.p2,
					.p3 {
						font-size: 22rpx;
						color: #8D8D8D;
						line-height: 30rpx;
						margin-top: 5rpx;
					}

					.p4 {
						text-align: right;
						margin-top: 10rpx;
						color: #8D8D8D;
						text-decoration: underline;
					}

				}
			}

			.total-price {
				font-size: 26rpx;
				line-height: 37rpx;
				text-align: right;
				margin-top: 14rpx;
			}

			.btns {
				margin-top: 20rpx;
				display: flex;
				justify-content: flex-end;

				.btn {
					width: 124rpx;
					height: 50rpx;
					border: 1px solid #2D4F43;
					font-size: 26rpx;
					border-radius: 10rpx;
					color: #2D4F43;
					text-align: center;
					line-height: 50rpx;
					margin-left: 32rpx;
				}
			}

		}
	}
</style>
